<template>
  <div>

    <div class="card" style="margin-bottom: 10px;height: 70px;align-content: center;">
      类别名称：<el-input v-model="data.params.categoryName" style="width: 300px; margin-right: 10px" placeholder="请输入类别名称查询"></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="info" style="margin: 0 10px" @click="reset">重置</el-button>
    </div>

    <div class="card" style="margin-bottom: 10px">
      <div style="margin-bottom: 10px">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>
      <el-table stripe :data="data.tableData">
        <el-table-column label="类别ID" prop="categoryId"></el-table-column>
        <el-table-column label="类别名称" prop="categoryName"></el-table-column>
        <el-table-column label="类别类型" prop="categoryType"></el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
        <el-table-column label="更新时间" prop="updateTime"></el-table-column>
        <el-table-column label="操作" align="center" width="160">
          <template #default="scope">
            <el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" @click="handleDelete(scope.row.categoryId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="card">
      <el-pagination 
        background 
        layout="total, sizes, prev, pager, next, jumper" 
        :page-sizes="[5, 10, 20, 50, 100]" 
        :page-size="data.params.pageSize" 
        v-model:current-page="data.params.pageNum" 
        :total="data.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog title="餐品类别信息" width="40%" v-model="data.formVisible" :close-on-click-modal="false" destroy-on-close>
      <el-form :model="data.form" label-width="100px" style="padding-right: 50px">
        <el-form-item label="类别名称" prop="categoryName">
          <el-input v-model="data.form.categoryName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="类别类型" prop="categoryType">
          <el-input v-model="data.form.categoryType" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="data.formVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">保 存</el-button>
      </span>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import request from "@/utils/request";
import {reactive, onMounted} from "vue";
import {ElMessageBox, ElMessage} from "element-plus";

const data = reactive({
  params: {
    categoryName: '',
    pageNum: 1,
    pageSize: 5
  },
  total: 0,
  formVisible: false,
  form: {},
  tableData: []
})

// 加载餐品类别数据
const load = () => {
  console.log('请求参数:', data.params)
  request.get('/seller/productCategory/page', {
    params: data.params
  }).then(res => {
    console.log('接口返回数据:', res)
    if (res.code === '200') {
      // 根据接口返回的数据结构，正确获取列表数据
      data.tableData = res.data.list || []
      data.total = res.data.total || 0
    } else {
      ElMessage.error(res.msg || '加载数据失败')
    }
  }).catch(err => {
    console.error('请求出错:', err)
    ElMessage.error('网络请求失败，请检查接口是否可用')
  })
}

// 重置查询条件
const reset = () => {
  data.params.categoryName = ''
  data.params.categoryType = ''
  load()
}

// 处理分页大小变化
const handleSizeChange = (size) => {
  data.params.pageSize = size
  load()
}

// 处理页码变化
const handleCurrentChange = (current) => {
  data.params.pageNum = current
  load()
}

// 新增餐品类别
const handleAdd = () => {
  data.form = {
    categoryType: ''
  }
  data.formVisible = true
}

// 编辑餐品类别
const handleEdit = (row) => {
  data.form = JSON.parse(JSON.stringify(row))
  data.formVisible = true
}

// 保存餐品类别
const save = () => {
  if (!data.form.categoryName) {
    ElMessage.warning('请输入类别名称')
    return
  }
  
  const url = data.form.categoryId ? '/seller/productCategory/update' : '/seller/productCategory/add'
  const method = data.form.categoryId ? 'put' : 'post'
  
  request[method](url, data.form).then(res => {
    if (res.code === '200') {
      ElMessage.success('保存成功')
      data.formVisible = false
      load()
    } else {
      ElMessage.error(res.msg || '保存失败')
    }
  })
}

// 删除餐品类别
const handleDelete = (id) => {
  if (!id) return
  
  ElMessageBox.confirm('删除后数据无法恢复，您确定删除吗?', '删除确认', { type: 'warning' }).then(() => {
    request.delete(`/seller/productCategory/delete/${id}`).then(res => {
      if (res.code === '200') {
        ElMessage.success('删除成功')
        load()
      } else {
        ElMessage.error(res.msg || '删除失败')
      }
    })
  }).catch(() => {
    // 取消删除操作
  })
}

// 页面加载时获取数据
onMounted(() => {
  load()
  // 不再使用模拟数据，只使用接口返回的真实数据
  // setTimeout(addMockData, 1000)
})
</script>